<?php
//header('Content-Type: text/html; charset=utf-8');


//SUZDAVANE NA NOSESHTIQ MASIV
$path_small = "img/men_small/";
$path_big = "img/men/";
$path_txt = "../img/men_text/";

$path_small_w = "img/women_small/";
$path_big_w = "img/women/";
$path_txt_w = "../img/women_text/";

//PROVERKA KUDE SE IZPULNQVA PHP FAILA I KOREKCIQ NA PUTQ
if(getcwd() == __DIR__) {
    $files = scandir("../".$path_small);
    $files_w = scandir("../".$path_small_w);
} else {
    $files = scandir($path_small);
    $files_w = scandir($path_small_w);
}
$pics_men = array();
$pics_women = array();



//MEN

//GETTING BASE NAMES
foreach ($files as $key => $value) {
    if ($key > 1) {
        $pics_men[($key-2)] = pathinfo($value, PATHINFO_FILENAME);
        $extension[($key-2)] = pathinfo($value, PATHINFO_EXTENSION);
    }
}

// 2nd LEVEL ARRAY STRUCTURE: [0]en_name, [1] loc_big, [2] loc_small, [3] bg_name, [4] descr
foreach($pics_men as $key => $value) {
    $pics_men[$key] = array('en_name' => $value);
    $pics_men[$key]['loc_small'] = $path_small.$value.'.'.$extension[$key];
    $pics_men[$key]['loc_big'] = $path_big.$value.'.'.$extension[$key];

    if(getcwd() == __DIR__) {
    $data = $path_txt.$value.'.txt';
    } else {
    $data = str_replace("../","",$path_txt.$value.'.txt');
    }

    if(file_exists($data)) {
        $result = file($data);
        $pics_men[$key]['bg_name'] = trim($result[0]);
        $pics_men[$key]['descr'] = trim($result[1]);
        $pics_men[$key]['price'] = number_format(floatval(trim($result[2])),2);
        $pics_men[$key]['sizes'] = trim($result[3]);
    }


}



//WOMEN

//GETTING BASE NAMES, add 300 to women shirt IDs
foreach ($files_w as $key => $value) {
    if ($key > 1) {
        $pics_women[($key-2)+300] = pathinfo($value, PATHINFO_FILENAME);
        $extension[($key-2)+300] = pathinfo($value, PATHINFO_EXTENSION);
    }
}

// 2nd LEVEL ARRAY STRUCTURE: [0]en_name, [1] loc_big, [2] loc_small, [3] bg_name, [4] descr
foreach($pics_women as $key => $value) {
    $pics_women[$key] = array('en_name' => $value);
    $pics_women[$key]['loc_small'] = $path_small_w.$value.'.'.$extension[$key];
    $pics_women[$key]['loc_big'] = $path_big_w.$value.'.'.$extension[$key];

    if(getcwd() == __DIR__) {
        $data = $path_txt_w.$value.'.txt';
    } else {
        $data = str_replace("../","",$path_txt_w.$value.'.txt');
    }

    if(file_exists($data)) {
        $result = file($data);
        $pics_women[$key]['bg_name'] = trim($result[0]);
        $pics_women[$key]['descr'] = trim($result[1]);
        $pics_women[$key]['price'] = number_format(floatval(trim($result[2])),2);
        $pics_women[$key]['sizes'] = trim($result[3]);
    }


}


//CREATE SPEEDY LOCATIONS ARRAY

$result = "";
$offices = 'includes/speedy_offices.txt';
if(file_exists($offices)) {
    $result = file($offices);
}

//echo '<pre>'.print_r($result, true).'</pre>';